package com.example.testapplication;

import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class Activity_alertdemo extends AppCompatActivity {
    private boolean[] checkedItems;//记录各列表项的状态
    private String[] items;//各列表项要显示的内容
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alertdemo);

        // 获取 显示带取消，确定按钮的对话框
        Button button1 = (Button) findViewById(R.id.button1);
        // 添加单击事件监听器
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 创建对话框对象
                AlertDialog alertDialog = new AlertDialog.Builder(Activity_alertdemo.this).create();
                alertDialog.setIcon(R.drawable.advise);
                alertDialog.setTitle("乔布斯");

                alertDialog.setMessage("活着就是为了改变世界，难道还有其他原因吗?");
                // 添加取消按钮
                alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "否", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(Activity_alertdemo.this, "您点击了否按钮", Toast.LENGTH_SHORT).show();
                    }
                });
                // 添加确定按钮
                alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(Activity_alertdemo.this, "您点击了是按钮", Toast.LENGTH_SHORT).show();
                    }
                });
                alertDialog.show(); // 显示对话框
            }
        });

        // 获取 显示带列表的对话框 按钮
        Button button2 = (Button) findViewById(R.id.buttonlist);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("Tag","点击了");
                // 创建名言字符串数组
                final String[] items = new String[]{"当你有使命，它会让你更专注", "要么出众，"
                        + "要么出局", "活着就是为了改变世界", "求知若饥，虚心若愚"};
                // 创建列表对话框对象
                AlertDialog.Builder builder = new AlertDialog.Builder(Activity_alertdemo.this);
                builder.setIcon(R.drawable.advise1);  // 设置对话框的图标
                builder.setTitle("请选择你喜欢的名言"); // 设置对话框的标题
                // 添加列表项
                builder.setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(Activity_alertdemo.this, "您选择"+items[i], Toast.LENGTH_SHORT).show();
                    }
                });
                builder.create().show(); // 创建对话框并显示

            }
        });
        // 获取 显示带单选列表项的对话框按钮
        Button button3 = (Button) findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 创建名字字符串数组
                final String[] items = new String[]{"扎克伯格", "乔布斯", "拉里.埃里森",
                        "安迪.鲁宾", "马云"};
                // 显示带单选列表项的对话框
                AlertDialog.Builder builder = new AlertDialog.Builder(Activity_alertdemo.this);
                builder.setIcon(R.drawable.advise2);
                builder.setTitle("如果让你选择，你最想做那个一个:");
                builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        // 显示选择结果
                        Toast.makeText(Activity_alertdemo.this, "您选择了"+ items[i], Toast.LENGTH_SHORT).show();

                    }
                });
                builder.setPositiveButton("确定",null); // 添加确定按钮
                builder.create().show(); // 创建对话框并显示

            }
        });

        // 获取显示带多选列表的对话框按钮
        Button button4 = (Button) findViewById(R.id.button4);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 记录各列表项的状态
                checkedItems = new boolean[]{false,true,false,true,false};
                // 各列表要显示的内容
                items = new String[]{"开心消消乐", "球球大作战", "欢乐斗地主",
                        "梦幻西游", "超级玛丽"};
                // 显示列表的对话框
                AlertDialog.Builder builder = new AlertDialog.Builder(Activity_alertdemo.this);
                builder.setIcon(R.drawable.advise2);
                builder.setTitle("请选择您喜爱的游戏");
                builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                        checkedItems[i] = b;  // 改变被操作列表项的状态
                    }
                });
                // 为对话框添加确定按钮
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String result = "";
                        for(int j = 0; j < checkedItems.length;j++){
                            if(checkedItems[j]){    // 当选项被选择中
                                result += items[j]+ ",";    // 将选项的内容添加到result中
                            }

                        }
                        // 当result不为空时，通过消息提示框显示选择的结果
                        if(!"".equals(result)){
                            // 去掉最后面“，”号
                            result = result.substring(0,result.length()-1);
                            Toast.makeText(Activity_alertdemo.this, "您选择了[" + result + "]", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                builder.create().show();
            }
        });
    }
}
